package com.shinhye.mobilelottery.ui.custom;

import com.shinhye.mobilelottery.R;
import com.shinhye.mobilelottery.utils.DensityUtil;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class MyGridView extends GridView {
	private PopupWindow pop;
	private TextView bigball;
	private OnTouchActionListener touchlistener;
	
	/**
	 * Register a callback to be invoked when a touch action event is sent to this view.
     * @param touchlistener the touch listener to attach to this view
	 */
	public void setOnTouchActionListener(OnTouchActionListener touchlistener) {
		this.touchlistener = touchlistener;
	}

	public MyGridView(Context context, AttributeSet attrs) {
		super(context, attrs);
		View popbg = View.inflate(context, R.layout.il_gridview_item_pop, null);
		bigball = (TextView) popbg.findViewById(R.id.ii_pretextView);
		
		pop = new PopupWindow(popbg);  //不能写成bigball！！
		pop.setBackgroundDrawable(null);
		
		pop.setAnimationStyle(0);
		int px = DensityUtil.dip2px(context, 55);
		pop.setWidth(px);
		pop.setHeight(px);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		int position = pointToPosition((int) ev.getX(), (int) ev.getY());
		if(position == INVALID_POSITION) {
			dismissPop();
			return true;
		}
		TextView ball = (TextView) getChildAt(position);
		
		switch(ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			this.getParent().getParent().requestDisallowInterceptTouchEvent(true);
			if(touchlistener != null)
				touchlistener.onActionDown(ball, position);
			showPop(ball);
			break;
		case MotionEvent.ACTION_MOVE:
			if(touchlistener != null)
				touchlistener.onActionMove(ball, position);
			updatePop(ball);
			break;
		case MotionEvent.ACTION_UP:
			this.getParent().getParent().requestDisallowInterceptTouchEvent(false);
			if(touchlistener != null)
				touchlistener.onActionUp(ball, position);
			dismissPop();
			break;
		default:
			dismissPop();
		}
		
		return super.onTouchEvent(ev);
	}

	private void showPop(TextView ball) {
		bigball.setText(ball.getText().toString());
		int xoff = -(bigball.getWidth() - ball.getWidth()) / 2;
		int yoff = -(ball.getHeight() + bigball.getHeight());
		pop.showAsDropDown(ball, xoff, yoff);
	}
	
	private void dismissPop() {
		if(pop.isShowing()) {
			pop.dismiss();
		}
	}
	
	private void updatePop(TextView ball) {
		if(pop.isShowing()) {
			bigball.setText(ball.getText().toString());
			int xoff = -(bigball.getWidth() - ball.getWidth()) / 2;
			int yoff = -(ball.getHeight() + bigball.getHeight());
			pop.update(ball, xoff, yoff, -1, -1);
		}
	}
	
	/**
	 * Call back when the touch event happened.
	 * @author ShinHye
	 * 2014-9-27 下午3:36:18
	 */
	public interface OnTouchActionListener {
		/**
		 * The touch down event happened
		 */
		void onActionDown(View view, int position);
		
		/**
		 * The touch move event happened
		 */
		void onActionMove(View view, int position);
		
		/**
		 * The touch up event happened
		 */
		void onActionUp(View view, int position);
	}
}
